Skip to main content

Longest repeating character replacement

Problem statement

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

Example 1:

Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa.

Using sliding window technique

/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var characterReplacement = function(s, k) {
let maxLen = 0;
let left = 0;
let maxCount = 0;

const freq = {};

for (let right = 0; right < s.length; right++) {
const char = s[right];
freq[char] = (freq[char] || 0) + 1;

maxCount = Math.max(maxCount, freq[char]);

while ((right - left + 1) - maxCount > k) {
freq[s[left]]--;
left++;
}

maxLen = Math.max(maxLen, right - left + 1);
}

return maxLen;
};